
var Haforms = {
	
	dragging:null,
	fullfeatured:true,
	moved:false,
	
	searchArea:function(area)
	{
		YAHOO.util.Event.addListener(window,"resize",this.onWindowResize);
		beenThere = {};

		this.inputs = inputs = area.getElementsByTagName("input");
		for (i in inputs) {
			if (inputs[i]!=null && beenThere[inputs[i].name]!=true) {
				beenThere[inputs[i].name]=true;
				if (inputs[i].type=="text") {
					this.inputTextInit(inputs[i]);
				}
			}
		}

		this.textareas = textareas = area.getElementsByTagName("textarea");
		for (i in textareas)
		{
			if (textareas[i]!=null && beenThere[textareas[i].name]!=true)
			{ 
				beenThere[textareas[i].name]=true;
				this.textareaInit(textareas[i]);
			}
		}

		this.els = els = area.getElementsByTagName("select");
		for (i in els) {
			if (els[i]!=null && els[i].beenThere!=true) {
				els[i].beenThere=true;
				YAHOO.util.Event.addListener(els[i],"click",this.onFocus);
				YAHOO.util.Event.addListener(els[i],"blur",this.onBlur);
				//YAHOO.util.Event.addListener(els[i],"mouseover",this.selectMEnter);
				//YAHOO.util.Event.addListener(els[i],"mouseout",this.selectMLeave);
				YAHOO.util.Event.addListener(els[i],"click",this.selectMLeave);
			}
		}

		this.els = els = area.getElementsByTagName("table");
		for (i in els) {
			if (els[i]!=null)
			if (els[i].tagName!=null && els[i].beenThere!=true) {
				els[i].beenThere=true;
				o = els[i];
				/*
				anchors = o.getElementsByTagName("a");
				for (u in anchors) {
					YAHOO.util.Event.addListener(anchors[u],"mousedown",this.tableHandleMD);
					YAHOO.util.Event.addListener(anchors[u],"mouseup",this.tableHandleMU);
				}
				*/
				YAHOO.util.Event.addListener(o,"mousedown",this.tableMouseDown);
				YAHOO.util.Event.addListener(o,"mouseup",this.tableMouseUp);
				YAHOO.util.Event.addListener(o,"mousemove",this.tableMouseMove);
				
			}
		}
	},
	
	/*
	selectMEnter:function(e) {
		o = e.target;		
		if (o.type!="select-one" && o.type!="select-multiple") o.selected = true;
		while ((o.type!="select-one" && o.type!="select-multiple") && o!=null) o = o.parentNode;
		o.style.position = "absolute";
		o.size = o.length;
	},

	selectMLeave:function(e) {
		o.style.position = "relative";
		o.size = 1;
	},
	*/
	
	tableHandleMD:function(e) 
	{
		Haforms.moved = false;
		Haforms.dragging = o = e.target;
		YAHOO.util.Event.stopEvent(e);
		YAHOO.util.Event.addListener(document,"mouseup",Haforms.tableHandleMU);
	},

	tableHandleMU:function(e) 
	{
		// uvolnění css stylů
		o = Haforms.dragging;
		for (c=0; o.tagName!="TABLE" && c++<10; c++) o = o.parentNode;
		YAHOO.util.Dom.removeClass(o,"canDragAt");

		for (c=0; o.tagName!="HTML" && c++<50; c++) o = o.parentNode;
		YAHOO.util.Dom.removeClass(o,"canNotDragAt");

		//uvolnění dokumentu
		YAHOO.util.Event.removeListener(document,"mouseup",Haforms.tableHandleMU);
		dragging = Haforms.dragging;
		
		//uvolnění přesouvané položky
		o = dragging;
		for (c=0; o.tagName!="TR" && c++<5; c++) o = o.parentNode;
		YAHOO.util.Dom.removeClass(o,"dragging");
		
		//uvolnění stavu
		Haforms.dragging = null;
		Haforms.cleanupLastDragPosition();
		if (Haforms.moved) { 
			YAHOO.util.Event.stopEvent(e); 
			if (e.target!=dragging) {
				controller.drag(dragging, e.target, Haforms.dragrel);
			}
		}
		Haforms.moved = false;
	},
	
	cleanupLastDragPosition:function() {
		if (Haforms.last!=null) {
			YAHOO.util.Dom.removeClass(Haforms.last,"dragBefore");
			YAHOO.util.Dom.removeClass(Haforms.last,"dragAfter");
		}				
	},
	
	tableMouseDown:function(e) 
	{
		if (e.target.tagName!="A" && Haforms.dragging==null) {
			o = e.target;
			for (c=0; o.tagName!="TR" && c++<5; c++) o = o.parentNode;
			if (YAHOO.util.Dom.hasClass(o,"selected")) {
				Haforms.select = false;
			} else {
				Haforms.select = true;
			}	
		
			document.getElementsByTagName("body").background="yellow";
			Haforms.tableMouseMove(e);
		} else Haforms.tableHandleMD(e);
		YAHOO.util.Event.stopEvent(e);
	},

	tableMouseUp:function(e) 
	{
		if (this.dragging==null) {
			Haforms.select = null;
		} else Haforms.tableHandleMU(e);
	},

	tableMouseMove:function(e) 
	{
		o = e.target;
		if (Haforms.dragging==null) {
			for (c=0; o.tagName!="TR" && c++<5; c++) o = o.parentNode;
			if (Haforms.select==true) {
				YAHOO.util.Dom.addClass(o,"selected");
				inputs = o.getElementsByTagName("input");
				for (i in inputs) {
					if (inputs[i].type=="checkbox" && e.target.type!="checkbox") {
						inputs[i].checked=true;
					}
				}
			} else if (Haforms.select==false) {
				YAHOO.util.Dom.removeClass(o,"selected");
				inputs = o.getElementsByTagName("input");
				for (i in inputs) {
					if (inputs[i].type=="checkbox" && e.target.type!="checkbox") {
						inputs[i].checked=false;
					}
				}
			}// else if (Haforms.select==null) o.style.background="yellow";		
		} else {
			if (Haforms.moved==false) {
				for (c=0; o.tagName!="TR" && c++<5; c++) o = o.parentNode;
				YAHOO.util.Dom.addClass(o,"dragging");
				for (c=0; o.tagName!="TABLE" && c++<10; c++) o = o.parentNode;
				YAHOO.util.Dom.addClass(o,"canDragAt");
				for (c=0; o.tagName!="HTML" && c++<50; c++) o = o.parentNode;
				YAHOO.util.Dom.addClass(o,"canNotDragAt");
				o = e.target;
			}
			for (c=0; o.tagName!="TR" && c++<5; c++) o = o.parentNode;
			Haforms.cleanupLastDragPosition();
			Haforms.last = o;
			elm = o;
			var x=0;

			epos = YAHOO.util.Event.getXY(e)[1];
			pos = YAHOO.util.Dom.getY(o);
			
			//document.getElementById("debugspace").innerHTML=(epos+" "+pos);
			if (epos>(pos+o.clientHeight/2)) {
				Haforms.dragrel = "After";
			} else {
				Haforms.dragrel = "Before";
			}
			YAHOO.util.Dom.addClass(o,"drag"+Haforms.dragrel);
		}	
		Haforms.moved = true;		
		YAHOO.util.Event.stopEvent(e);
	},
	
	onFocus:function(e) {
		e = YAHOO.util.Event.getTarget(e);
		YAHOO.util.Dom.addClass(e.parentNode,"focused");
	},
	
	onBlur:function(e) {
		e = YAHOO.util.Event.getTarget(e);
		YAHOO.util.Dom.removeClass(e.parentNode,"focused");
	},

	inputTextInit:function(t)
	{
		YAHOO.util.Event.addListener(t,"focus",this.onFocus);
		YAHOO.util.Event.addListener(t,"blur",this.onBlur);
		if (t.size==0) return;
		if (YAHOO.util.Dom.hasClass(t,"maxwidth") || this.fullfeatured) {
			t.charWidth=t.clientWidth/t.size;
			t.style.width="100%";
		}
		if (YAHOO.util.Dom.hasClass(t,"idealfontsize") || this.fullfeatured) {
			YAHOO.util.Event.addListener(t,"keyup",this.inputOnKeyUp);
			t.originalHeight = t.clientHeight;
			if (YAHOO.util.Dom.hasClass(t,"notproportional")) {
				t.maxKoef=1.8;
			} else {
				t.maxKoef=1.2;
			}			
			this.setIdealFontSize(t);
		}	
	},

	inputOnKeyUp:function(e)
	{
		t = e.target;
		Haforms.setIdealFontSize(t);
	},
	
	setIdealFontSize:function(t)
	{
		size = (t.clientWidth / t.charWidth);
		r=Math.ceil((t.value.length / size));
		if (r>t.maxKoef) { 
			size = t.originalHeight/r+"px";
			YAHOO.util.Dom.setStyle(t,"fontSize",size);
		} else {
			size = (t.originalHeight*0.8)+"px";
			YAHOO.util.Dom.setStyle(t,"fontSize",size);
		}
		if (navigator.appName=="Opera" || navigator.appName=="Microsoft Internet Explorer") {
			YAHOO.util.Dom.replaceClass(t,"_stupidBrowser");
		}
	},

	textareaInit:function(t)
	{
		YAHOO.util.Event.addListener(t,"focus",this.onFocus);
		YAHOO.util.Event.addListener(t,"blur",this.onBlur);
		t.minRows = t.rows;		
		if (YAHOO.util.Dom.hasClass(t,"maxwidth") || this.fullfeatured) {
			t.columnWidth=t.clientWidth/t.cols;
			t.style.width="100%";
		}
		if (YAHOO.util.Dom.hasClass(t,"writetabs") || this.fullfeatured) {
			YAHOO.util.Event.addListener(textareas[i],"keyup",this.insertTab);
			YAHOO.util.Event.addListener(textareas[i],"keydown",this.insertTab);
		}
		if (YAHOO.util.Dom.hasClass(t,"idealrows") || this.fullfeatured) {
			YAHOO.util.Event.addListener(textareas[i],"keypress",Haforms.textareaOnKeyUp);
			//ano, tohle je pakárna. pokud víš, jak to spravit napiš
			this.setIdealRows(t);
			this.setIdealRows(t);
		}
  	},
  	
  	/**
	  * Insert a tab at the current text position in a textarea
	  * Jan Dittmer, jdittmer@ppp0.net, 2005-05-28
	  * Inspired by http://www.forum4designers.com/archive22-2004-9-127735.html
	  * Tested on: 
	  *   Mozilla Firefox 1.0.3 (Linux)
	  *   Mozilla 1.7.8 (Linux)
	  *   Epiphany 1.4.8 (Linux)
	  *   Internet Explorer 6.0 (Linux)
	  * Does not work in: 
	  *   Konqueror (no tab inserted, but focus stays)
	  * 
	  * Modified to work with YUI by Michal Hantl (http://hantl.info)
	  * 
	  */
  	insertTab:function(event)
  	{
  		obj = YAHOO.util.Event.getTarget(event);
		var tabKeyCode = 9;
		if (event.which) // mozilla
			var keycode = event.which;
		else // ie, ff
			var keycode = event.keyCode;
		if (keycode == tabKeyCode) {
			if (event.type == "keydown") {
				if (obj.setSelectionRange) {
					// mozilla
					var s = obj.selectionStart;
					var e = obj.selectionEnd;
					obj.value = obj.value.substring(0, s) + 
						"\t" + obj.value.substr(e);
					obj.setSelectionRange(s + 1, s + 1);
					obj.focus();
				} else if (obj.createTextRange) {
					// ie
					document.selection.createRange().text="\t"
					obj.onblur = function() { this.focus(); this.onblur = null; };
				} else {
					// unsupported browsers
				}
			}
			if (event.returnValue) // ie ?
				event.returnValue = false;
			if (event.preventDefault) // dom
				event.preventDefault();
			YAHOO.util.Event.stopEvent(e);
		}
	},
  	
  	/*
  	prcWidth:function(t) {
		width=t.clientWidth;
		//t.style.float="left";
		t.style.width="auto"; //="none";
		//t.style.display="inline";
		t.colWidth=t.clientWidth/t.cols;
		t.style.width = (prc = lastprc = 50)+"%";
		cycles = 0;
		while (true) {
			if (t.clientWidth<width) {
				prc = prc+(lastprc = lastprc/2);
			} else if (t.clientWidth>width) {
				prc = prc-(lastprc = lastprc/2);
			}
			t.style.width=prc+"%";
			if (cycles++>20 || Math.abs(t.clientWidth-width)<1) {
				break;
			}	
		}
		//alert("prc : "+prc+" c:"+cycles);		
  	},
  	*/ 
  	
  	textareaOnKeyUp:function(e)
  	{
  		t = e.target;
		Haforms.setIdealRows(t);		
  	},

  	onWindowResize:function(e)
  	{
		for (i in this.textareas) {
			if (textareas[i].parentNode!=null) {
				Haforms.setIdealRows(textareas[i]);
			}
		}
  	},

	setIdealRows:function(t)
	{
		cols = t.clientWidth/t.columnWidth;
		//alert("ClW :"+t.clientWidth+" ColW:"+t.columnWidth);
		lines = t.value.split('\n');
		rows = lines.length;
		for (i=0; i<lines.length; i++)
		{
			plus=Math.floor(lines[i].length/cols);
			//if (YAHOO.util.Dom.hasClass(t,"notproportional")) plus=Math.ceil(plus*0.9);
			if (plus>1)
			{
				words = lines[i].split(' ');
				for (wi in words)
				{
					//ie dlouhé slova automaticky zalomí, ostatní ne
					if (words[wi].length>cols && navigator.appName!="Microsoft Internet Explorer") plus--;
				}
				if (plus>0) rows+=plus;
			}
		}
		
		if (Math.ceil(rows*1.05)>(rows+3)) {
			rows = Math.ceil(rows*1.05)+2;
		} else rows = rows+3;
		
		if ((rows)<t.minRows) {
			t.rows = t.minRows; 
		} else {
			t.rows = rows;
		}
		if (navigator.appName=="Microsoft Internet Explorer") {
			window.focus();
			t.focus();
		}
  	}
  	
}